// project created on 11/03/2007 at 16:09
using System;
using System.Collections.Generic;
using System.IO;

public class BuildCleaner
{
	private List<string> _args = null;
	
	public static void Main(string[] args)
	{
		BuildCleaner build = new BuildCleaner((IList<string>)args);
		build.Run();
	}
	
	public BuildCleaner(IList<string> args)
	{
		_args = new List<string>(args);
	}
	
	public void Run()
	{
		int mode = 0;
		
		if(_args.IndexOf("--c") != -1)
		{
			ConsoleColor c = Console.ForegroundColor;
			Console.ForegroundColor = ConsoleColor.Yellow;
			Console.WriteLine("using current directory ..");
			Console.ForegroundColor = c;
			mode = 2;
		}
		else if(_args.IndexOf("--help") != -1)
		{
			mode = 1;
		}		
		else
		{
			mode = 3;
		}
		
		switch(mode)
		{
			case 0:
			
				showHelp();
				return;
			case 1:
			
				showHelp();
				return;
			case 2:
			
				RemoveOn(Environment.CurrentDirectory);
			
			break;
			case 3:
			{
				RemoveOn(string.Join(" " ,_args.ToArray()));
			}
			break;
		}
	}
	
	private void showHelp()
	{
		Console.WriteLine("Makepkg Cleaner");
		Console.WriteLine("This tool remove the pkg/ and src/ from the specified directory tree");
		Console.WriteLine();
		Console.WriteLine("usage:");
		Console.WriteLine("mkpcleaner [options]");
		Console.WriteLine("--c	cleanup from the current directory");
		Console.WriteLine("/path/to/clean  cleanup from the specified point");
		Console.WriteLine("--help show this help");
		return;
	}
	
	private void RemoveOn(string path)
	{
		DirectoryInfo pathInfo = new DirectoryInfo(path);
		
		DirectoryInfo[] dirs = pathInfo.GetDirectories();
		
		foreach(DirectoryInfo current in dirs)
		{
			
			
			if(current.Name.ToLower() == "src")
			{
				current.Delete();
				Console.WriteLine("Removing {0}",current.FullName);
			}
			else if(current.Name.ToLower() == "pkg")
			{
				current.Delete();
				Console.WriteLine("Removing {0}",current.FullName);
			}
			else
				RemoveOn(current.FullName);
		}
	}
	
}